#include <stdio.h>
#include <string.h>

#include "LLGEN.H"
#include "LLAPP.H"

int test(struct List* l);
void populate_list(struct List* l, char* line);
void clear_list(struct List* l);

int main(int argc, char* argv[])
{
	char line[128] = "12321\0";

	struct List* l = CreateLList(CreateData2, DeleteData2, DuplicatedNode2, NodeDataCmp2);

	while (1)
	{
		printf("Insert a string: ");
		gets_s(line, 128);
		populate_list(l, line);
		int result = test(l);
		if (result == 0)
		{
			printf("No\n");
		}
		else
		{
			printf("Yes\n");
		}
		clear_list(l);
	}
}

int test(struct List* l)
{
	Link p1 = l->LHead;
	Link p2 = l->LTail;

	int result = 0;

	while (p1 != NULL && p2 != NULL)
	{
		if (p1 == p2 || p1->prev == p2)
		{
			// survive the comparision
			result = 1;
			break;
		}

		if (NodeDataCmp2(p1->pdata, p2->pdata) != 0)
		{
			// not equal, failed
			result = 0;
			break;
		}

		p1 = p1->next;
		p2 = p2->prev;
	}

	return result;
}

void populate_list(struct List* l, char* line)
{
	const int length = strlen(line);
	for (int i = 0; i < length; i++)
	{
		// insert order doesn't affect the result
		char temp[2];
		temp[0] = line[i];
		temp[1] = 0;
		AddNodeAtHead(l, temp);
	}
}

void clear_list(struct List* l)
{
	Link p = l->LHead;
	while (p != NULL)
	{
		const Link temp = p;
		p = p->next;
		temp->prev = NULL;
		temp->next = NULL;
		DeleteData2(temp->pdata);
		temp->pdata = NULL;
	}
	l->LHead = NULL;
}
